<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Folder
 *
 * @author QuangKhoi
 */
class Folder {

    //put your code here
    /**
     * 
     * @var Int
     */
    public $folderId;

    /**
     * Determine folder's owner
     * @var Int
     */
    public $userId;

    /**
     *
     * @var String
     */
    public $folderName;

    /**
     * Folders' parent
     * @var Int
     */
    public $parentId;

    /**
     * Includes: Company | Competitors | Market Terms | Social Account | Save Searches
     * @var Int
     */
    public $folderType;

    /**
     *
     * @var Folder
     */
    public $child;

    /**
     * Constructor
     */
    public function __construct() {
        
    }

    /**
     * Read folder infos from the database
     * @param Int $id
     */
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM folders WHERE folderid = '$id'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->folderId = $rows["folderid"];
                $instance->folderName = $rows["foldername"];
                $instance->userId = $rows["userid"];
                $instance->parentId = $rows["parentid"];
                $instance->folderType = $rows["foldertype"];
                return $instance;
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any folder with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    /**
     * Create new Folder object to store
     * @param Int $userid
     * @param String $folderName
     * @param Int $parentId
     */
    public static function withData($userid, $folderName, $parentId, $folderType) {
        $instance = new self();
        $instance->userId = $userid;
        $instance->folderName = $folderName;
        $instance->parentId = $parentId;
        $instance->folderType = $folderType;
        return $instance;
    }
    
    public static function getId($name) {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM folders WHERE foldername = '$name'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                return $rows["folderid"];
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any folder with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public static function listFolder($userId) {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM folders WHERE userid = '$userId' OR userid = '0'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $folders = array();
                foreach ($result->dataTable->rows as $row) {
                    $folder = new self();
                    $folder->folderId = $row["folderid"];
                    $folder->folderName = $row["foldername"];
                    $folder->userId = $row["userid"];
                    $folder->parentId = $row["parentid"];
                    $folder->folderType = $row["foldertype"];
                    $folders[] = $folder;
                }
                return $folders;
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any folder with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

}

?>
